package com.kunbo.cn.controller.core.purchase;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kunbo.cn.entity.core.purchase.Waybill;
import com.kunbo.cn.service.core.purchase.WaybillService;
import com.kunbo.cn.util.json.FastJsonUtils;
import com.kunbo.cn.util.retu.BuildBean;
import com.kunbo.cn.util.retu.CommonResult;
import com.kunbo.cn.util.retu.ResultBean;

@RestController
@RequestMapping("/purchase/waybill")
public class WaybillController {
	@Autowired
	private WaybillService waybillService;

	@RequestMapping("/add")
	public CommonResult add(String jsonstr) {
		if (StringUtils.isNotBlank(jsonstr)) {
			return new CommonResult(false, "参数为空", null);
		}
		try {
			Waybill w = FastJsonUtils.toBean(jsonstr, Waybill.class);
			waybillService.save(w);
			return new CommonResult(true, "操作成功", null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new CommonResult(false, "数据转换异常，请检测", null);
		}

	}

	@RequestMapping("/findById")
	public Waybill findById(String id) {
		return waybillService.findById(id);

	}

	// 获取所有订单编号
	@RequestMapping("/getAllOrder")
	public ResultBean<List<Map<String, Object>>> getAllOrder() {
		ResultBean<List<Map<String, Object>>> result = new ResultBean<List<Map<String, Object>>>();
		return BuildBean.buildResult(result, "成功", "失败", waybillService.getAllOrder());
	}

	// 通过订单号获取物料名称
	@RequestMapping("/getMaterielName")
	public String getMaterielName(@RequestParam("getMaterielName") String getMaterielName) {
		return waybillService.getMaterielName(getMaterielName);
	}

	// 通过订单id获取物料名称
	@RequestMapping("/getOrderIdGetMaterial")
	public String getOrderIdGetMaterial(@RequestParam("id") Integer id) {
		return waybillService.getOrderIdGetMaterial(id);
	}

	// 算出某个订单的余量
	@RequestMapping("/computationAllowance")
	public String computationAllowance(@RequestParam("orderCode") String orderCode,
			@RequestParam("freightVolume") float freightVolume, @RequestParam("judge") String judge) {
		return waybillService.computationAllowance(orderCode, freightVolume, judge);
	}

	@RequestMapping("/orderTransfer")
	public ResultBean<Boolean> orderTransfer(@RequestParam("orderId") String orderId,
			@RequestParam("orderIds") String orderIds, @RequestParam("number") Float number) {
		ResultBean<Boolean> result = new ResultBean<Boolean>();
		return BuildBean.buildResults(result, "success", "error", waybillService.orderTransfer(orderId, orderIds, number));
	}

	@RequestMapping("/getOrderCode")
	public String getOrderCode(@RequestParam("waybillId") Integer waybillId) {
		return waybillService.getOrderCode(waybillId);
	}

	// 通过订单号获取物料名称
	@RequestMapping("/getMaterielNames")
	public String getMaterielNames(@RequestParam("getMaterielName") String getMaterielName) {
		return waybillService.getMaterielNames(getMaterielName);
	}
	
	@RequestMapping("/getallowance")
	public float getallowance(
			@RequestParam("id")Integer id
			) {
		return waybillService.getallowance(id);
	}
	
	@PostMapping("/waybillOrderCount")
	public ResultBean<Boolean> waybillOrderCount(
			@RequestParam("id")String id,
			@RequestParam("count")float count,
			@RequestParam("state")String state
			){
		ResultBean<Boolean>  result   = new ResultBean<Boolean>();
		return BuildBean.buildResult(result, "succes", "error", waybillService.waybillOrderCount(id,count,state));
	}
	
	
	@PostMapping("/updWayBillStatus")
	public ResultBean<Boolean> updateWayBillStatus(
			@RequestParam("tableName")String tableName,
			@RequestParam("id")Integer id,
			@RequestParam("stateCode")String stateCode
			){
		ResultBean<Boolean> result  = new ResultBean<Boolean>();
		return BuildBean.buildResult(result, "success", "error", waybillService.updateWayBillStatus(tableName,id,stateCode));
	}
	
	

}
